﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameTank3D
{
    public class Menu : Dialog
    {
        SpriteBatch spritebatch;
        Texture2D texture;
        Texture2D[] itemtextureNormal;
        Texture2D[] itemtextureSelect;
        MenuItem[] menuitem;
        int n;
        public Vector2 position;
        public Rectangle rectangle;
        Vector2 itemsize;
        Vector2 menusize;
        //**For animation**//
        float velocity = 0;
        float acceleraion = 1;
        float t = 0;
        int isFreeFall = 1;
        int isDaoDongTatDan = 0;

        public Menu(Game game, Texture2D background, Texture2D[] itemNormal, Texture2D[] itemSelect)
            : base(game)
        {
            // TODO: Construct any child components here
            spritebatch = (SpriteBatch) Game.Services.GetService(
                                            typeof (SpriteBatch));;
            texture = background;
            itemtextureNormal = itemNormal;
            itemtextureSelect = itemSelect;
            n = itemtextureNormal.Length;
            menuitem = new MenuItem[n];
            itemsize = new Vector2(itemtextureNormal[0].Width * 2 / 3, itemtextureNormal[0].Height * 2 / 3);
            menusize = new Vector2(texture.Width * 2 / 3, texture.Height * 2 / 3);
            position = new Vector2((Game.Window.ClientBounds.Width - menusize.X) / 2, 0);
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width * 2 / 3, texture.Height * 2 / 3);

            for (int i = 0; i < n; i++)
            {
                menuitem[i] = new MenuItem(Game, spritebatch, position + new Vector2((menusize.X - itemsize.X) / 2, (menusize.Y - n * (itemsize.Y + 10)) / 2 + (itemsize.Y + 10) * i), itemtextureNormal[i], itemtextureSelect[i]);
                menuitem[i].Initialize();
            }
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (isFreeFall == 1)
            {
                velocity += acceleraion;
                position.Y += velocity;
                rectangle.Y = (int)position.Y;
                if (position.Y >= (Game.Window.ClientBounds.Height - menusize.Y) * 3 / 5)
                {
                    velocity = Math.Abs(velocity);
                    isFreeFall = 0;
                    isDaoDongTatDan = 1;
                }
            }
            if (isDaoDongTatDan == 1)
            {
                t += MathHelper.ToRadians(10);
                position.Y += Math.Abs(velocity) * (float)Math.Cos(t) * -1;
                rectangle.Y = (int)position.Y;
                velocity -= 0.05f;
                if (velocity <= 0)
                {
                    isDaoDongTatDan = 2;
                    velocity = 5;
                }
            }
            if (isDaoDongTatDan == 2)
            {
                t += MathHelper.ToRadians(10);
                float deltaY = Math.Abs(velocity) * (float)Math.Cos(t) * -1;
                float deltaX = rectangle.X * 1.0f / rectangle.Y * deltaY;
                position.Y += deltaY;
                position.X += deltaX;
                rectangle.Y = (int)position.Y;
                rectangle.X = (int)position.X;
                rectangle.Width += (int)deltaX * -2;
                rectangle.Height += (int)deltaY * -2;
                menusize.Y = rectangle.Height;
                menusize.X = rectangle.Width;
                velocity -= 0.007f;
                if (velocity <= 0)
                {
                    isDaoDongTatDan = 0;
                }
            }
            for (int i = 0; i < n; i++)
            {
                menuitem[i].position = this.position + new Vector2((menusize.X - itemsize.X) / 2, (menusize.Y - n * (itemsize.Y + itemsize.Y * 0.2f)) / 2 + (itemsize.Y + itemsize.Y * 0.2f) * i);
                menuitem[i].Update(gameTime);
            }
            MouseState mouseState = Mouse.GetState();
            for (int i = 0; i < n; i++)
            {
                if (menuitem[i].rectangle.Contains(mouseState.X, mouseState.Y))
                {
                    menuitem[i].IsSelect = true;

                    if (mouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                    {
                        switch (i)
                        {
                            case 0:
                                {
                                    
                                } break;
                            case 1:
                                {
                                    
                                } break;
                            case 2:
                                {
                                    
                                } break;
                            case 3:
                                {
                                    
                                } break;
                            case 4:
                                {
                                    Game.Exit();
                                } break;
                        }
                    }
                }
                else
                {
                    menuitem[i].IsSelect = false;
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spritebatch.Draw(texture, rectangle, Color.White);
            for (int i = 0; i < n; i++)
                menuitem[i].Draw(gameTime);
            base.Draw(gameTime);
        }
    }
}
